<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
 * Description of category_model
 *
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_banner_model extends CI_Model {

	//put your code here


	function __construct() {
		parent::__construct();
		log_message('debug', 'Init Model Initialized');
	}

	//--get_banner Single --//
	function get_banner($size=null){
		$sql = "Select * from banner where status=51 and size = '$size' order by RAND() limit 1";
		$query = $this->db->query($sql);
		return $query->row();
	}
	
	
	//--Get Banners from Banner Table (Rahul)--//
	function get_banners($featured = false, $status =1, $num=NULL, $offset = NULL, $limit=1,$impression =NULL, $clicks =NULL, $expire=NULL){


		if(!$impression){
			$sql = "Select * from banner where status=$status order by id desc limit $offset,$num ";
		}
		if($impression==1){
			$sql = "Select * from banner where status=$status order by impressions desc limit $offset,$num";
		}
		if(!$clicks){
			$sql = "Select * from banner where status=$status order by id desc limit $offset,$num";
		}
		if($clicks==1){
			$sql = "Select * from banner where status=$status order by clicks desc limit $offset,$num";
		}
		if($expire==1){
			$sql = "Select *from banner where clicks>=max_clicks or impressions>=max_impressions order by id desc";

		}


		//print $clicks;
		//print $sql;

		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	//--Get Banners from Banner Table (Rahul)--//
	function get_inactive_banners($featured = false, $status =51, $num=NULL, $offset = NULL, $limit=1,$impression =NULL, $clicks =NULL, $expire=NULL){
	
	
		if(!$impression){
			$sql = "Select * from banner where status=$status order by id desc limit $offset,$num ";
		}
		if($impression==1){
			$sql = "Select * from banner where status=$status order by impressions desc limit $offset,$num";
		}
		if(!$clicks){
			$sql = "Select * from banner where status=$status order by id desc limit $offset,$num";
		}
		if($clicks==1){
			$sql = "Select * from banner where status=$status order by clicks desc limit $offset,$num";
		}
		if($expire==1){
			$sql = "Select *from banner where clicks>=max_clicks or impressions>=max_impressions order by id desc";
	
		}
	
	
		//print $clicks;
		//print $sql;
	
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}

	//--Insert Banner --//
	function insert_banner($param) {
		if($param){
			$this->db->insert('banner',$param);
		}
	}

	//--Delete Banner --//
	function delete_banner($id,$image_file) {
		if($id && $image_file){
			$sql="delete from banner where id = $id";
			$this->db->query($sql);
			//--Delete Image File --//

			$f = BASEPATH.'../assets/images/data/advertisement/'.$image_file;
			//$f = base_url()."assets/images/data/advertisement/".$image_file;

			@unlink($f);
			
			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $image_file);
			
			$d =  strpos($image_file, '_');
			
			$n = substr($image_file,0,$d);
			
			$f = BASEPATH.'../assets/images/data/advertisement/'.$n.'.'.$ext;
			//$f = base_url()."assets/images/data/advertisement/".$image_file;
			
			@unlink($f);
			
			
		}
	}
	function search($search=null){
		$sql = "Select * from banner where title like '%$search%' order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $query->result();
		}else{
			return false;
		}
	}
		function change_status($id, $status) {
			if ($status == 1) {
				$sql = "update banner set status=51 where id=$id";
			}
			if ($status == 51) {
				$sql = "update banner set status=1 where id=$id";
			}
			$this->db->query($sql);
		}

		
	
	function get_banner_by_id($id){
		if (isset($id)) {
			$sql="select * from banners where id=$id";
			$query=$this->db->query($sql);
			$result=$query->row();
			return $result;
		}
	}

	function get_banner_details($id=NULL) {
		$sql = "select * from banner where id=$id";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
}